package com.ctg.behavior.calc.impala.transfer.userlist;

import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Component;

import com.alibaba.fastjson.JSON;
import com.ctg.behavior.calc.common.CtSQLConsts;
import com.ctg.behavior.calc.common.CtSQLParam;
import com.ctg.behavior.calc.impala.CtSQLTransfer;
import com.ctg.behavior.calc.utils.CtCalcPropertyUtils;
import com.ctg.behavior.common.calc.CtCalc;
import com.ctg.behavior.common.userslist.CtULCalcBody;

import lombok.extern.slf4j.Slf4j;

@Slf4j
@Component
public class CtFunnelUserListTransfer implements CtSQLTransfer {

	/******************************************************
	 * @apiNote:
	 */
	@Override
	public String doTransfer(CtCalc calcObject, CtSQLParam sqlParam) {

		if (!(calcObject instanceof CtULCalcBody)) {
			log.warn("calcObject is not CtULCalcBody, class={}", calcObject.getClass());
			log.warn("calcObject: {}", JSON.toJSON(calcObject));
			return "";
		}
		CtULCalcBody ugulReport = (CtULCalcBody) calcObject;
		log.info("ugulReport={}", JSON.toJSONString(ugulReport));

		// 此层SQL的表查询别名
		String thisAlias = "ugul";
		String userAlias = "uguser";

		StringBuilder impalaSQL = new StringBuilder();
		{
			// 1. SELECT部分; 若profiles数组不为空,则需要关联user表
			// impalaSQL.append(" SELECT DISTINCT ");
			impalaSQL.append(" SELECT ");
			impalaSQL.append(thisAlias).append(".user_id AS userId ");
			impalaSQL.append(" , max(").append(thisAlias).append(".device_id) AS deviceId ");
			for (String profile : ugulReport.getProfiles()) {
				String userProperty = CtCalcPropertyUtils.getProperty(profile);
				impalaSQL.append(" , ").append(userAlias).append(".").append(userProperty).append(" AS ").append(userProperty);
			}
		}
		{
			// 2. 根据APP_ID来决定查询哪个表; 若profiles数组不为空,则需要关联user表
			impalaSQL.append(" FROM ");
			String appId = ugulReport.getAppId();
			impalaSQL.append(" a_event_parquet_").append(appId).append(" ").append(thisAlias);
			// if (!ugulReport.getProfiles().isEmpty()) {
			// 要与user表关联
			impalaSQL.append(" LEFT JOIN ");
			impalaSQL.append(" a_user_kudu_").append(appId).append(" ").append(userAlias);
			impalaSQL.append(" ON ").append(userAlias).append(".user_id = ").append(thisAlias).append(".user_id ");
			// }
			impalaSQL.append(" WHERE 1=1 ");
		}
		{
			//
			impalaSQL.append(" AND ").append(userAlias).append(".user_id IN ( ");
			impalaSQL.append(sqlParam.getParam(CtSQLConsts.KEY_FILTER_SQL));
			impalaSQL.append(" ) ");
		}
		// 6. GROUP BY
		{
			impalaSQL.append(" GROUP BY ");
			impalaSQL.append(thisAlias).append(".user_id ");
			for (String profile : ugulReport.getProfiles()) {
				String userProperty = CtCalcPropertyUtils.getProperty(profile);
				impalaSQL.append(" , ").append(userAlias).append(".").append(userProperty);
			}
		}
		// 7. 排序、LIMIT
		if (!StringUtils.isEmpty(ugulReport.getSortByField())) {
			String userProperty = CtCalcPropertyUtils.getProperty(ugulReport.getSortByField());
			impalaSQL.append(" ORDER BY ").append(userProperty);
			if (ugulReport.getAsc()) {
				impalaSQL.append(" ASC ");
			} else {
				impalaSQL.append(" DESC ");
			}
		} else {
			impalaSQL.append(" ORDER BY ").append(thisAlias).append(".user_id ");
		}

		// 8. LIMIT 或 分页
		if (ugulReport.getLimit() >= 0) {
			Integer currentPage = ugulReport.getCurrentPage();
			currentPage = currentPage - 1;
			currentPage = (currentPage > 0 ? currentPage : 0);
			Integer startPos = currentPage * ugulReport.getPageSize();
			Integer endPos = (1 + currentPage) * ugulReport.getPageSize();
			if (endPos > ugulReport.getLimit() && ugulReport.getLimit() != 0) {
				endPos = ugulReport.getLimit();
			}
			Integer limitSize = endPos - startPos;
			limitSize = limitSize > 0 ? limitSize : 0;
			impalaSQL.append(" LIMIT ").append(limitSize).append(" OFFSET ").append(startPos);
		}

		String retSQL = impalaSQL.toString();
		log.info("CalcSQL={}", retSQL);

		return retSQL;
	}

}
